package com.chuanjiabao.wisdom.ui.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.WindowManager;

import com.chuanjiabao.wisdom.MainActivity;
import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.bean.ReasionInfoCallbackBean;
import com.chuanjiabao.wisdom.bean.RefreshMessageEvent;
import com.chuanjiabao.wisdom.bean.SecretbackBean;
import com.chuanjiabao.wisdom.databinding.ActivitySetAndChangePwdBinding;
import com.chuanjiabao.wisdom.ui.ActivityStack;
import com.chuanjiabao.wisdom.ui.BaseActivity;
import com.chuanjiabao.wisdom.util.GlideUtil;
import com.chuanjiabao.wisdom.util.GsonUtil;
import com.chuanjiabao.wisdom.util.HTTPAPI;
import com.chuanjiabao.wisdom.util.SpUtils;
import com.chuanjiabao.wisdom.util.StringUtil;
import com.chuanjiabao.wisdom.util.ToastUtil;
import com.chuanjiabao.wisdom.util.dialog.ArriveDialog;
import com.lzy.okgo.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;

import okhttp3.Call;
import okhttp3.Response;

public class SetAndChagePwdActivity extends BaseActivity {

    ActivitySetAndChangePwdBinding binding;

    ReasionInfoCallbackBean.DataBean bean;
    boolean isOpen = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_set_and_change_pwd);
        binding.setActivity(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            getWindow().setStatusBarColor(getResources().getColor(R.color.F2C3749));
        }
        initView();
    }

    private void initView() {
        bean = (ReasionInfoCallbackBean.DataBean) SpUtils.getBean("module_reasion");
        if (0 != bean.getSecret_on()) {
            isOpen = false;
            binding.tvModuleTextStyle.setText("已开启");
            GlideUtil.setImgMethod(
                    R.drawable.icon_pwd_open,
                    binding.ivModuleChange
            );
        } else {
            isOpen = true;
            binding.tvModuleTextStyle.setText("已关闭");
            GlideUtil.setImgMethod(
                    R.drawable.icon_pwd_close,
                    binding.ivModuleChange
            );
        }
        binding.rlModuleBack.setOnClickListener(this);
        binding.ivModuleRight.setOnClickListener(this);
        binding.llModuleBack.setOnClickListener(this);
        binding.ivModuleChange.setOnClickListener(this);
        binding.llModuleChangePwd.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.llModuleBack:
            case R.id.tvModuleTextStyle:
                finish();
                break;
            case R.id.ivModuleRight:
                ArriveDialog arriveDialog = new ArriveDialog(this);
                arriveDialog.show();
                arriveDialog.setOnBHClickListener(position -> {
                    RefreshMessageEvent event = new RefreshMessageEvent();
                    event.setType("main");
                    event.setId(position +"");
                    EventBus.getDefault().post(event);
                    ActivityStack.getScreenManager().popAllActivityExceptOne(MainActivity.class);
                });
                break;
            case R.id.ivModuleChange:
                if (isOpen) {
                    isOpen = false;
                    GlideUtil.setImgMethod(
                            R.drawable.icon_pwd_open,
                            binding.ivModuleChange
                    );
                    setSerection();
                } else {
                    isOpen = true;
                    GlideUtil.setImgMethod(
                            R.drawable.icon_pwd_close,
                            binding.ivModuleChange
                    );
                    setSerection();
                }
                break;
            case R.id.llModuleChangePwd:
                startActivity(
                        new Intent(
                                SetAndChagePwdActivity.this,
                                ChangeOldSecretPwdActivity.class
                        )
                );
                break;
        }
    }

    int p = 0;

    public void setSerection() {
        HTTPAPI.getInstance().setSerection(p + "", new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                if (!StringUtil.isFail(s)) {
                    StringUtil.showFail(SetAndChagePwdActivity.this, s);
                } else {
                    SecretbackBean secretbackBean = GsonUtil.GsonToBean(s, SecretbackBean.class);
                    if (0 == secretbackBean.getData().getSecret_on()) {
                        ToastUtil.showToast("关闭成功");
                        binding.tvModuleTextStyle.setText("已关闭");
                    } else {
                        ToastUtil.showToast("开启成功");
                        binding.tvModuleTextStyle.setText("已开启");
                    }
                    bean.setSecret_on(secretbackBean.getData().getSecret_on());
                    SpUtils.putBean("module_reasion", bean);
                }
            }
        });
    }

}
